package com.app.service.Impl;

import com.app.common.CalculateDistance;
import com.app.mapper.SceneryMapper;
import com.app.model.CategoryModel;
import com.app.model.SceneryModel;
import com.app.model.TownshipModel;
import com.app.service.CategoryService;
import com.app.service.SceneryService;
import com.app.service.TownService;
import com.app.vo.SceneryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;

@Service
public class SceneryServiceImpl implements SceneryService {
    @Autowired
    private SceneryMapper sceneryMapper;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private TownService townService;
    @Override
    public void update(SceneryModel model) {
        sceneryMapper.updateById(model);
    }

    @Override
    public void insert(SceneryModel model) {
        sceneryMapper.insert(model);
    }

    @Override
    public void deleteById(Integer id) {
        sceneryMapper.deleteById(id);
    }

    @Override
    public SceneryVo selectList(Integer page, Integer limit, String keyword, Integer type, Double longitude, Double latitude, Integer township) {
        SceneryVo vo = new SceneryVo();
        page = (page - 1) * limit;
        List<SceneryModel> sceneryModelList = sceneryMapper.selectSceneryList(page, limit, keyword, type, township);
        for (int i = 0; i < sceneryModelList.size(); i++) {
            if (sceneryModelList.get(i).getType() != null) {
                CategoryModel categoryById = categoryService.getCategoryById(sceneryModelList.get(i).getType());
                sceneryModelList.get(i).setTypeText(categoryById.getName());
            }
            String[] split = sceneryModelList.get(i).getSwiperImage().split(",");
            sceneryModelList.get(i).setSwiperImages(Arrays.asList(split));
            if (sceneryModelList.get(i).getPrice() != null && sceneryModelList.get(i).getPrice().equals("0")) {
                sceneryModelList.get(i).setPrice("免费");
            }
            if (sceneryModelList.get(i).getLongitude() != null && sceneryModelList.get(i).getLatitude() != null) {
                double longitude1 = Double.parseDouble(sceneryModelList.get(i).getLongitude());
                double latitude1 = Double.parseDouble(sceneryModelList.get(i).getLatitude());

                if (latitude != null && longitude != null) {
                    Double haversine = CalculateDistance.haversine(latitude1, longitude1, latitude, longitude);
                    if (haversine != 0) {
                        DecimalFormat df = new DecimalFormat("0.0");
                        String formatted = df.format(haversine);
                        sceneryModelList.get(i).setDistance(formatted + "km");
                    }
                } else {
                    sceneryModelList.get(i).setDistance("暂无");
                }
            } else {
                sceneryModelList.get(i).setDistance("暂无");
            }
        }
        List<TownshipModel> townList = townService.getTownList();
        for (int i = 0; i < sceneryModelList.size(); i++) {
            if (townList != null) {
                for (int i1 = 0; i1 < townList.size(); i1++) {
                    if (sceneryModelList.get(i).getTownship() != null && sceneryModelList.get(i).getTownship().equals(townList.get(i1).getId())) {
                        sceneryModelList.get(i).setTownshipText(townList.get(i1).getTownshipName());
                    }
                }
            }
        }
        int count = sceneryMapper.selectSceneryCount(keyword, type);
        vo.setList(sceneryModelList);
        vo.setCount(count);
        return vo;
    }

    @Override
    public SceneryVo getList(Integer page, Integer limit, String keyword, Integer type, Double longitude, Double latitude, Integer township) {
        SceneryVo vo = new SceneryVo();
        page = (page - 1) * limit;
        List<SceneryModel> sceneryModelList = sceneryMapper.getSceneryList(page, limit, keyword, type, township);
        for (int i = 0; i < sceneryModelList.size(); i++) {
            if (sceneryModelList.get(i).getType() != null) {
                CategoryModel categoryById = categoryService.getCategoryById(sceneryModelList.get(i).getType());
                sceneryModelList.get(i).setTypeText(categoryById.getName());
            }
            String[] split = sceneryModelList.get(i).getSwiperImage().split(",");
            sceneryModelList.get(i).setSwiperImages(Arrays.asList(split));
            if (sceneryModelList.get(i).getPrice() != null && sceneryModelList.get(i).getPrice().equals("0")) {
                sceneryModelList.get(i).setPrice("免费");
            }
            if (sceneryModelList.get(i).getLongitude() != null && sceneryModelList.get(i).getLatitude() != null) {
                double longitude1 = Double.parseDouble(sceneryModelList.get(i).getLongitude());
                double latitude1 = Double.parseDouble(sceneryModelList.get(i).getLatitude());

                if (latitude != null && longitude != null) {
                    Double haversine = CalculateDistance.haversine(latitude1, longitude1, latitude, longitude);
                    if (haversine != 0) {
                        DecimalFormat df = new DecimalFormat("0.0");
                        String formatted = df.format(haversine);
                        sceneryModelList.get(i).setDistance(formatted + "km");
                    }
                } else {
                    sceneryModelList.get(i).setDistance("暂无");
                }
            } else {
                sceneryModelList.get(i).setDistance("暂无");
            }
        }
        List<TownshipModel> townList = townService.getTownList();
        for (int i = 0; i < sceneryModelList.size(); i++) {
            if (townList != null) {
                for (int i1 = 0; i1 < townList.size(); i1++) {
                    if (sceneryModelList.get(i).getTownship() != null && sceneryModelList.get(i).getTownship().equals(townList.get(i1).getId())) {
                        sceneryModelList.get(i).setTownshipText(townList.get(i1).getTownshipName());
                    }
                }
            }
        }
        int count = sceneryMapper.getSceneryCount(keyword, type);
        vo.setList(sceneryModelList);
        vo.setCount(count);
        return vo;
    }

    @Override
    public SceneryModel selectById(Integer id) {
        SceneryModel sceneryModel = sceneryMapper.selectById(id);
        if (sceneryModel.getType() != null) {
            CategoryModel categoryById = categoryService.getCategoryById(sceneryModel.getType());
            sceneryModel.setTypeText(categoryById.getName());
        }
        String[] split = sceneryModel.getSwiperImage().split(",");
        sceneryModel.setSwiperImages(Arrays.asList(split));
        if (sceneryModel.getPrice() != null && sceneryModel.getPrice().equals("0")) {
            sceneryModel.setPrice("免费");
        }
        return sceneryModel;
    }
}
